package toanstt.com.gameplay;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import toanstt.com.MatchUp.MainActivity;
import toanstt.com.MatchUp.Notice;
import toanstt.com.MatchUp.Sound2;
import toanstt.com.MatchUp.T;
import toanstt.com.MatchUp.TBitmap2;
import toanstt.com.MatchUp.Vec2;

public class CellTable 
{
	Paint paint_white;
	int temp_x,temp_y;
	int action, actionCode;
	int x,y;
	int selectx = -1,selecty =-1;
	TBitmap2 CELLS;
	//TBitmap2 MAGIC;
	static int INDEX[][];
	static int INDEX_KICK[][];
	Bitmap CELL_BITMAP[][];
	int slipwidth[];
	int delay=0;
	public static  int COUNT_DONE;
	int NUMBOM=10;
	int CHECK_WIN;
	int CHECK_WIN_COUNT;
	public CellTable()
	{
		paint_white = new Paint();
		paint_white.setColor(Color.BLACK);
		
	}
	void CreateINDEX()
	{
		INDEX = new int[T.GAME_COL][T.GAME_ROW];
		INDEX_KICK = new int[T.GAME_COL][T.GAME_ROW];
		for(int i =0;i<T.GAME_COL;i++)
			for(int j =0;j<T.GAME_ROW;j++)
			{
				INDEX[i][j] =0;
				INDEX_KICK[i][j] = -1;
			}
		int a,b;
		for(int i =0;i<NUMBOM;i++)
		{
			a = T.random.nextInt(T.GAME_COL);
			b = T.random.nextInt(T.GAME_ROW);
			while(INDEX[a][b]==-1)
			{
				a = T.random.nextInt(T.GAME_COL);
				b = T.random.nextInt(T.GAME_ROW);
			}
			INDEX[a][b] = -1;
		}
		int dem=0;
		for(int i =0;i<T.GAME_COL;i++)
			for(int j =0;j<T.GAME_ROW;j++)
			{
				if(INDEX[i][j] == 0)
				{
					dem=0;
					for(int i1=i-1;i1<=i+1;i1++)
					{
						if(i1>=0 && i1 <T.GAME_COL )
						for(int j1=j-1;j1<=j+1;j1++)
						{
							if(j1>=0 && j1 <T.GAME_ROW )
							if(INDEX[i1][j1]==-1)
								dem++;
						}
					}
					INDEX[i][j] = dem;
				}
			}
		int j =0;
		CHECK_WIN= T.GAME_COL*T.GAME_ROW -  NUMBOM;
		CHECK_WIN_COUNT =0;
	}
	public void NewGame(int i1,int i2)
	{
		T.GAME_COL =9;
		T.GAME_ROW =9;
		NUMBOM=1;
		if(i1==1) NUMBOM=12;
		else if(i1==2) NUMBOM=14;
		else if(i1==3) NUMBOM=16;
		CreateINDEX();
		GameStatus.LEV = "LEVEL "+(i1+1)+":"+i2;
		

		
		T.CELL_WIDTH = (T.DISPLAY_WIDTH -20)/T.GAME_COL;
		T.GAME_MINY =(T.DISPLAY_HEIGHT-50)/2 - T.GAME_ROW*T.CELL_WIDTH/2;
		T.GAME_MINY = (int)(T.GAME_MINY*0.8f);
		
		T.GAME_HEIGHT =T.GAME_ROW* T.CELL_WIDTH;
		T.GAME_WIDTH=T.GAME_COL* T.CELL_WIDTH;
		T.GAME_MINX = T.DISPLAY_WIDTH/2 -T.GAME_WIDTH/2;
		
		
		CELLS = new TBitmap2("NUM.png", 3, 4, T.CELL_WIDTH);

		T.STATUS_MINX = T.GAME_MINX + T.GAME_WIDTH;
		T.STATUS_MINY = 20;
		COUNT_DONE =0;
		IS_LOSE=-1;
	}
	int n;
	
	int i,j;
	int i2,i3,j3;
	int IS_LOSE=-1;
	public void Draw(Canvas canvas)
	{
		if(IS_LOSE>=0)
		{
			IS_LOSE++;
			if(IS_LOSE == 15)
			{
				T.notice.DoNotice(Notice.LOSE, 0);
			}
		}
		if(CHECK_WIN_COUNT == CHECK_WIN)
		{
			int ti = (int)G.time/1000;
			int tti =100;
			switch(T.GAME_I1)
			{
			case 0:tti=100;break;
			case 1:tti=120;break;
			case 2:tti=140;break;
			case 3:tti=150;break;
			}
			int type2 =1;
			if(ti/tti==0)type2=3;
			else if(ti/tti==1)type2=2;
			else if(ti/tti==2)type2=1;
			T.GAME_DATA[T.GAME_I1][T.GAME_I2] = type2;
			T.notice.DoNotice(Notice.FINISH, type2);
			
			CHECK_WIN_COUNT++;
		}
		if(check_dem >=0 && ismousedown == true)
		{
			check_dem++;
			if(check_dem ==10)
			{
				if(check_dx==dx && check_dy == dy)
				{
				
					if(INDEX_KICK[check_dx][check_dy] == -1)
						INDEX_KICK[check_dx][check_dy]=0;
					else INDEX_KICK[check_dx][check_dy] = -1;
					
					
				}
				check_dx = check_dy =dx=dy=-1;
				check_dem=-1;
			}
		}
		canvas.drawRect(T.GAME_MINX, T.GAME_MINY, T.GAME_MINX + T.GAME_WIDTH, T.GAME_MINY + T.GAME_HEIGHT, paint_white);
		for(i=0;i<T.GAME_COL;i++)
		{
			
				
			for(j=0;j<T.GAME_ROW;j++)
			{
				if(INDEX_KICK[i][j] == -1)
					canvas.drawBitmap(CELLS.bitmap[9], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
				else if(INDEX_KICK[i][j] == 0)
				{
					canvas.drawBitmap(CELLS.bitmap[9], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
					canvas.drawBitmap(CELLS.bitmap[10], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
				}
				else if(INDEX_KICK[i][j] == 3)
				{
					canvas.drawBitmap(CELLS.bitmap[11], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
					canvas.drawBitmap(CELLS.bitmap[10], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
				}
				else if(INDEX[i][j] == -1)
					canvas.drawBitmap(CELLS.bitmap[11], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
				else
					canvas.drawBitmap(CELLS.bitmap[INDEX[i][j]], T.GAME_MINX + i*T.CELL_WIDTH, T.GAME_MINY + j*T.CELL_WIDTH, null);
			}
		
		}
		
		
	}
	int dx,dy;
	int count;
	int i5,j5;
	void DeQui(int x,int y)
	{
		if(x <0 || x >= T.GAME_COL)
			return;
		if(y <0 || y >= T.GAME_ROW)
			return;
		INDEX_KICK[x][y] =1;
		//CHECK_WIN_COUNT++;
		if(INDEX[x][y] ==0)
		{
			
			//Log.d("aaaa","a"+CHECK_WIN_COUNT + "/" + CHECK_WIN);
			for(int x1 = x-1;x1<=x+1;x1++)
			{
				for(int y1 = y-1;y1<=y+1;y1++)
				{
					if(x1 >=0 && x1 <T.GAME_COL && y1 >=0 && y1 <T.GAME_ROW)
					{
						if(INDEX[x1][y1] ==0 && INDEX_KICK[x1][y1]==-1 )
							DeQui(x1,y1);
						else
						{
							INDEX_KICK[x1][y1]=1;
							//CHECK_WIN_COUNT++;
						}
					}
				}
			}
		}
	}
	int check_dx=-1;
	int check_dy=-1;
	int check_dem=-1;
	boolean ismousedown = false;
	public void touch(MotionEvent mo)
	{
		
		if(IS_LOSE >=0)
			return;
		action = mo.getAction();
		actionCode = action & MotionEvent.ACTION_MASK;
		temp_x = (int)mo.getX();
		temp_y = (int)mo.getY();
		dx = temp_x-T.GAME_MINX;
		//Log.d("aaaaaaa","ddddddddddddd"+action);
		if(action == MotionEvent.ACTION_DOWN)
		{
			if(dx>=0 && dx <=T.GAME_WIDTH)
			{
				dy = temp_y-T.GAME_MINY;
				if(dx>=0 && dy <=T.GAME_HEIGHT && dy>=0)
				{
					dx = dx/T.CELL_WIDTH;
					dy = dy/T.CELL_WIDTH;
					if(INDEX_KICK[dx][dy] ==1)
						return;
					check_dx = dx;
					check_dy = dy;
					check_dem=0;
					ismousedown = true;
				}
			}
		}
		/*else if(action == MotionEvent.ACTION_MOVE)
		{
			//check_dem++;
			if(dx>=0 && dx <=T.GAME_WIDTH)
			{
				dy = temp_y-T.GAME_MINY;
				if(dx>=0 && dy <=T.GAME_HEIGHT && dy>=0)
				{
					dx = dx/T.CELL_WIDTH;
					dy = dy/T.CELL_WIDTH;
					//if(INDEX_KICK[dx][dy] ==1)
						//return;
					
					if(dx!=check_dx || dy!=check_dy)
					{
						check_dem=-1;
						if(check_dx!=-1 && check_dy != -1)
						{
							if(INDEX_KICK[check_dx][check_dy] == -1)
								INDEX_KICK[check_dx][check_dy]=0;
							else INDEX_KICK[check_dx][check_dy] = -1;
							
							check_dx = check_dy =-1;
							
						}
					}
				}
			}
		}*/
		else if(action == MotionEvent.ACTION_UP)
		{
			ismousedown = false;
			if(dx>=0 && dx <=T.GAME_WIDTH)
			{
				dy = temp_y-T.GAME_MINY;
				if(dx>=0 && dy <=T.GAME_HEIGHT && dy>=0)
				{
					dx = dx/T.CELL_WIDTH;
					dy = dy/T.CELL_WIDTH;
					if(INDEX_KICK[dx][dy] ==1)
						return;
					if(check_dx ==-1 || check_dy ==-1)
						return;
					if(INDEX[dx][dy] >= 0)
					{
						if(dx == check_dx && dy == check_dy)
						{
							DeQui(dx,dy);
							CheckWin();
						}
					}
					else if(INDEX[dx][dy] == -1)
					{
						IS_LOSE =0;
						for(int i=0;i<T.GAME_COL;i++)		
							for(int j=0;j<T.GAME_ROW;j++)
							{
								if(INDEX[i][j] ==-1)
								{
									if(INDEX_KICK[i][j] ==0)
										INDEX_KICK[i][j] =3;
									else
										INDEX_KICK[i][j] = 1;
								}
							}
					}
						
				}
			}
		}
			
	}
	int ll,jj;
	private void CheckWin()
	{
		CHECK_WIN_COUNT=0;
		for(ll=0;ll<T.GAME_ROW;ll++)
			for(jj=0;jj<T.GAME_ROW;jj++)
			{
				if(INDEX_KICK[ll][jj] ==1)
					CHECK_WIN_COUNT++;
			}
		Log.d("checkin",""+CHECK_WIN_COUNT);
	}
	void LoadCards()
	{
		
	}
}
